<?php


namespace app\base\service;

use think\admin\service\AdminService;

use think\Cache;

/**
 * Heyge视频生成服务
 * Class AdminService
 * @package think\admin\service
 */
class HeygenService extends AdminService
{

    protected $authorization = '';

    protected $url = 'https://api.heygen.com/';

    protected function getAuthorization($user_id)
    {
        $this->authorization = $this->app->db->name('ai_api_config')
            ->where(['user_id' => $user_id,'type'=>4])->value('access_key');
        if(empty($this->authorization)){
            return $this->resulet(0,'请先配置authorization');
        }
    }

    protected function resulet($code=0,$msg='',$data=[])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

    //视频生成
    public function createVideo($user_id,$audio_url='',$avatar_id="Annie_expressive12_public",$width="1280",$height="720")
    {
        //$avatar_id = "416ebb05db2649258a4457daf6694090";
      //  $avatar_id = "Annie_expressive12_public";
        if(empty($avatar_id)){
            $avatar_id = "Annie_expressive12_public";
        }
        $this->getAuthorization($user_id);
        $url = $this->url."v2/video/generate";
        $data = [
            "video_inputs"=>[
                [
                    "character"=>[
                        'type'=>"avatar",
                        'avatar_id'=>$avatar_id,
                        'avatar_style'=>'normal'
                    ],
                    'voice'=>[
                        'type'=>'audio',
                        'audio_url'=>$audio_url,
                       // 'voice_id'=>"1985984feded457b9d013b4f6551ac94"
                    ]
                ]
            ],
            'dimension'=>[
                'width'=>$width,
                'height'=>$height
            ]

        ];

        $options['headers'] = ['X-Api-Key:'.$this->authorization,'Content-Type: application/json'];
//        $options['proxy'] = "127.0.0.1:7897";
        //return $data;
      //  dump(json_encode($data));
        $res = http_post($url,json_encode($data),$options);
        $res = json_decode($res,true);
        if($res){
            if(isset($res['data']['video_id']) && !empty($res['data']['video_id'])){
                return $this->resulet(1,'success',$res['data']['video_id']);
            }else{
                return $this->resulet(0,$res['error']['message']);
            }
        }else{
            return $this->resulet(0,'请求失败');
        }


    }


    //查询视频状态
    public function getVideoStatus($user_id,$video_id='')
    {
        $this->getAuthorization($user_id);
        $url = $this->url."v1/video_status.get";
        $data = [
            "video_id"=>$video_id
        ];

        $options['headers'] = ['X-Api-Key:'.$this->authorization,'Content-Type: application/json'];
//        $options['proxy'] = "127.0.0.1:7897";
        $res = http_get($url,$data,$options);
        $res = json_decode($res,true);
        if($res){
            if($res['message']=="Success"){
                return $this->resulet(1,'success',$res['data']);
            }else{
                return $this->resulet(0,$res['error']['message']);
            }
        }else{
            return $this->resulet(0,'请求失败');
        }
    }

    //查询我的视频模板列表
    public function getVideoTemplate($user_id)
    {
        $this->getAuthorization($user_id);
        $url = $this->url."v2/avatars";
        $options['headers'] = ['X-Api-Key:'.$this->authorization,'Content-Type: application/json'];
//        $options['proxy'] = "127.0.0.1:7897";
        $res = http_get($url,null,$options);
        $res = json_decode($res,true);
        return $res;
    }
}
